﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Threading;
using Client.App.Services;
using Client.App.ViewModels.Interfaces;
using Core.Mvvm.ViewModels;
using Multiprocessing.Core.Units;

namespace Client.App.ViewModels.Implementations
{
    public class ConfigurationViewModel : ClosableViewModel, IConfigurationViewModel
    {
        #region Fields

        private int _maxProcessingUnitCount;
        private readonly IConfigurationService _configurationService;
        private readonly IList<IProcessingBlockViewModel> _proccessingBlockTypes;
        private IProcessingBlockViewModel _selectedProcessingBlockType;
        private int _attemptCount;

        #endregion

        #region Properties

        public int AttemptCount
        {
            get { return _attemptCount; }
            set
            {
                SetProperty(ref _attemptCount, value, () => this.AttemptCount);
                _configurationService.SetAttemptCount(value);
            }
        }

        public int MaxProcessingBlockSize
        {
            get { return _maxProcessingUnitCount; }
            set
            {
                SetProperty(ref _maxProcessingUnitCount, value, () => MaxProcessingBlockSize);
                _configurationService.SetMaxProcessingBlockSize(value);
            }
        }

        public IList<IProcessingBlockViewModel> ProcessingBlockTypes
        {
            get { return _proccessingBlockTypes; }
        }

        public IProcessingBlockViewModel SelectedProcessingBlockType
        {
            get { return _selectedProcessingBlockType; }
            set 
            {
                SetProperty(ref _selectedProcessingBlockType, value, () => this.SelectedProcessingBlockType);
                _configurationService.SetProcessingBlockType(value.Type);
            }
        }

        #endregion

        #region Constructors

        public ConfigurationViewModel(Dispatcher dispatcher, IConfigurationService configurationService) 
            : base(dispatcher)
        {
            if (ReferenceEquals(configurationService, null))
            {
                throw new ArgumentNullException("configurationService");
            }
            _configurationService = configurationService;
            _maxProcessingUnitCount = configurationService.GetMaxProcessingBlockSize();
            _attemptCount = configurationService.GetAttemptCount();
            _proccessingBlockTypes = new List<IProcessingBlockViewModel>();
            LoadProcessingBlockTypes();
        }
        
        #endregion

        #region Methods

        private void LoadProcessingBlockTypes()
        {
            _proccessingBlockTypes.Add(new ProcessingBlockViewModel("Task-base модуль", typeof(TaskProcessingBlock)));
            _proccessingBlockTypes.Add(new ProcessingBlockViewModel("Thread-base модуль", typeof(TaskProcessingBlock)));
        }

        #endregion
    }
}
